﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;


namespace TranTekBE.KPI_NhanSu
{
    public partial class Them_Lich_Lam_Viec_Nhan_Vien : PagePopUp
    {
       

        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (!IsPostBack)
            {
                // Get user
                FunctionsUtils.GetDropListUser(droplist_ten_nhan_vien);
                // Get stores
                FunctionsUtils.GetDropListStore(droplist_cua_hang_lam_viec);
                
                lblNgayLamViec.Text = FunctionsUtils.GetValueStringFromQueryString("dateadd", "0");
                droplist_cua_hang_lam_viec.SelectedValue = UserInfo.Stores;
              
            }
        }

        protected void btnAddSchedule_Click(object sender, EventArgs e)
        {
            if (droplist_ten_nhan_vien.SelectedIndex != 0 && droplist_cua_hang_lam_viec.SelectedIndex != 0 && lblNgayLamViec.Text != "" && lblNgayLamViec.Text != null)
            {
                string id_nhan_vien = droplist_ten_nhan_vien.SelectedValue.ToString();
                string get_user_name = "";
                string sql_get_user = @"
                select C001900 as IdUser, C001905+' '+C001904 as NameUser, C001901 as UserName,CONVERT(varchar(max),C001903,2) as Permission,
                C001909 as Stores
                from T0019 
                where C001900 = '" + id_nhan_vien + @"'
                ";
                DataSet sql_popup_user = FunctionsUtils.Database.ExecuteQuery(sql_get_user);
                if (sql_popup_user != null)
                {
                    //
                    get_user_name = sql_popup_user.Tables[0].Rows[0]["UserName"].ToString();
                    
                }

                
                string ten_nhan_vien = droplist_ten_nhan_vien.SelectedItem.Text;
                string cua_hang_lam_viec = droplist_cua_hang_lam_viec.SelectedValue.ToString();
                int ca_lam_viec = Convert.ToInt32(radio_btn_ca.SelectedValue.ToString());
                DateTime ngay_lam_viec = Convert.ToDateTime(lblNgayLamViec.Text.ToString());

                if (CheckDataExist(id_nhan_vien, ca_lam_viec, ngay_lam_viec))
                {
                    try
                    {
                        // Insert thông tin lịch làm việc người dùng 
                        string sql_add_user_schedule = @"
                                    INSERT INTO tbl_LichLamViec
                                    (ID_USER,NAME_USER,CUA_HANG_DANG_LAM_VIEC,CA_LAM_VIEC,NGAY_THEM_VAO,NGUOI_THEM_VAO)
                                    VALUES
                                    ('" + id_nhan_vien + "','"
                                        + get_user_name + "','"
                                                + cua_hang_lam_viec + "',"
                                                + ca_lam_viec + ",'"
                                                + lblNgayLamViec.Text.ToString() + "','"
                                                + UserInfo.UserName
                                                + "')";

                        FunctionsUtils.Database2.ExecuteNonQuery(sql_add_user_schedule);
                        ClosePopup(true);
                        
                    }
                    catch (Exception ex)
                    {
                        ltrErr.Text = ex.ToString();
                        ltrErr.ForeColor = System.Drawing.Color.Red;
                    }
                }
                else {
                    ltrErr.Text = "Lỗi! Đã tồn tại lịch làm việc của nhân viên " + ten_nhan_vien;
                    ltrErr.ForeColor = System.Drawing.Color.Red;
                }
            }
            else
            {
                ltrErr.Text = "Lỗi! Vui lòng kiểm tra lại";
                ltrErr.ForeColor = System.Drawing.Color.Red;
            }
        }

        private bool CheckDataExist(string idusername, int ca, DateTime day)
        {
            //string sql_check_stores = "select C007000 as IDCuaHang ,C007001 as CuaHang from T0070  where C007001 not in ('RACHMIEU','PMT')";
            //DataSet ds_store = FunctionsUtils.Database.ExecuteQuery(sql_check_stores);
            string str_sosanh_store = "CUA_HANG_DANG_LAM_VIEC in ( ";
            for (int i = 1; i < droplist_cua_hang_lam_viec.Items.Count; i++)
            {
                if (i == (droplist_cua_hang_lam_viec.Items.Count - 1))
                    str_sosanh_store += "'" + droplist_cua_hang_lam_viec.Items[i].Value + "'" + " ) ";

                else
                    str_sosanh_store += "'" + droplist_cua_hang_lam_viec.Items[i].Value + "',";


            }
            string sql_so_sanh = @"select * from  tbl_LichLamViec 
                  where ID_USER = '" + idusername + @"' and " + str_sosanh_store + @"
                  and  CA_LAM_VIEC = " + ca + @"
                  and NGAY_THEM_VAO = '" + day.ToShortDateString() + @"'";

            DataSet ds = FunctionsUtils.Database2.ExecuteQuery(sql_so_sanh);

            return FunctionsUtils.isDataSetNullOrEmpty(ds);



        }
        




    }
}